package DMSXL._2024.BinaryTrees;

import java.util.ArrayList;
import java.util.List;

public class findMode {
    public static void main(String[] args) {
        Integer[] nums = {1,null,2,null,null,2};
        TreeNode root = CreateTree.createBinaryTree(nums,0);
        int[] result = new findMode().findMode(root);
        for (int i = 0; i < result.length; i++) {
            System.out.println(result[i]);
        }
    }
    int max_k = 1;
    int temp_k = 1;
    TreeNode pre;

    List<Integer> list = new ArrayList<>();
    public int[] findMode(TreeNode root) {
        inorder(root);
        return list.stream().mapToInt(Integer::intValue).toArray();
    }
    public void inorder(TreeNode cur){
        if(cur == null){
            return;
        }
        inorder(cur.left);
        if(pre != null){
            if(pre.val == cur.val){
                temp_k++;
            }else {
                temp_k = 1;
            }
        }

        if(temp_k > max_k){
            max_k = temp_k;
            list.clear();
            list.add(cur.val);
        }else if(temp_k == max_k){
            list.add(cur.val);
        }

        pre = cur;
        inorder(cur.right);
    }

}
